﻿// --------------------------------------------------------------------------------------------------------------------- 
// <copyright file="ProjectSelector.cs" company="Microsoft.Blueprints.Contrib Team">
//   (C) 2009 Microsoft.Blueprints.Contrib Team
// </copyright>
// <summary>
//   Defines the ProjectSelector type.
// </summary>
// ---------------------------------------------------------------------------------------------------------------------
namespace Microsoft.Blueprints.Contrib.Dialogs
{
    #region Using Directives

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Windows.Forms;

    using EnvDTE;

    using SoftwareFactories.Blueprints;

    #endregion

    /// <summary>
    /// Reusable Blueprint dialog for selecting a 
    /// Visual Studio Project to execute Blueprint logic against.
    /// </summary>
    public partial class ProjectSelector : Form
    {
        /// <summary>
        /// Internal instance of the Blueprint Manager Context.
        /// </summary>
        private readonly BPMContextBase context;

        /// <summary>
        /// Initializes a new instance of the <see cref="ProjectSelector"/> class.
        /// </summary>
        public ProjectSelector()
        {
            this.InitializeComponent();
        }

        /// <summary>
        /// Initializes a new instance of the <see cref="ProjectSelector"/> class.
        /// </summary>
        /// <param name="context">
        /// The context.
        /// </param>
        public ProjectSelector(BPMContextBase context) : this()
        {
            this.context = context;
        }

        /// <summary>
        /// Gets SelectedProject.
        /// </summary>
        /// <value>
        /// The selected project.
        /// </value>
        public Project SelectedProject
        {
            get
            {
                return this.projectList.SelectedItem as Project;
            }
        }

        /// <summary>
        /// Populates the SelectedProject combo box with a list of the 
        /// Visual Studio projects in the current solution.
        /// </summary>
        public void PopulateProjectList()
        {
            List<Project> projects = new List<Project>();

            foreach (Project project in this.context.DTE.Solution.Projects)
            {
                projects.Add(project);
            }

            List<string> validProjectsKinds = new List<string>
                                                  {
                                                          "{54435603-DBB4-11D2-8724-00A0C9A8B90C}", // PROJECT_KIND_VSNET_SETUP
                                                          VSLangProj.PrjKind.prjKindCSharpProject
                                                  };

            // Filter out project types we don't care about
            var visibleProjects = (from project in projects where validProjectsKinds.Contains(project.Kind) select project).ToList();

            this.projectList.DisplayMember = "Name";
            this.projectList.DataSource = visibleProjects;

            this.projectList.SelectedIndex = -1;
            this.projectList.Text = "Please Select a Visual Studio Project:";
        }

        /// <summary>
        /// Executed when the Cancel button is clicked.
        /// </summary>
        /// <param name="sender">
        /// The sender of the event.
        /// </param>
        /// <param name="e">
        /// The event that sender raised.
        /// </param>
        private void OnCancelButtonClicked(object sender, EventArgs e)
        {
            this.projectList.SelectedItem = null;
            this.Close();
        }

        /// <summary>
        /// Executed when the OK button is clicked.
        /// </summary>
        /// <param name="sender">
        /// The sender of the event.
        /// </param>
        /// <param name="e">
        /// The event that sender raised.
        /// </param>
        private void OnOkButtonClicked(object sender, EventArgs e)
        {
            if (this.projectList.SelectedIndex == -1)
            {
                MessageBox.Show("Please Select a valid Visual Studio Project");
            }
            else
            {
                this.Close();
            }
        }
    }
}